package com.pig4cloud.pig.homecenter.dto;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Data;

import java.io.IOException;

@Data
public class EntityStateDTO {

	@JsonAlias("entity_id")
	private String entityId;
	private String state;
	@JsonDeserialize(using = EntityStateDTO.CustomAny2StrDeserializer.class)
	private String attributes;

	public static class CustomAny2StrDeserializer extends JsonDeserializer<String> {

		@Override
		public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
			JsonNode node = p.getCodec().readTree(p);

			return node.toString();
		}
	}
}
